package com.solarfridge.testclient;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;

public class TestClientWindow {

	public static String URL = "http://127.0.0.1:8888/fridge_report";
	protected Shell shell;
	private Text txtFridgeId;
	private Text txtFreq;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TestClientWindow window = new TestClientWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new GridLayout(2, false));
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Fridge Id: ");
		
		txtFridgeId = new Text(shell, SWT.BORDER);
		txtFridgeId.setText("TestFridge1");
		txtFridgeId.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		
		Button btnTestConnection = new Button(shell, SWT.NONE);
		btnTestConnection.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				MonitorConnection connection = new MonitorConnection(URL, txtFridgeId.getText());
				MessageBox result = new MessageBox(shell);
				result.setText("Result of the test");
				result.setMessage("The test finished with result: " + 
						String.valueOf(connection.testConnection()));
				result.open();
			}
		});
		btnTestConnection.setText("Test Connection");
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		new Label(shell, SWT.NONE);
		
		Button btnSpoofData = new Button(shell, SWT.NONE);
		btnSpoofData.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
			}
		});
		btnSpoofData.setText("Start Spoofing Data");
		
		Label lblFrequencymin = new Label(shell, SWT.NONE);
		lblFrequencymin.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblFrequencymin.setText("Frequency (min): ");
		
		txtFreq = new Text(shell, SWT.BORDER);
		txtFreq.setText("1");
		txtFreq.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
		new Label(shell, SWT.NONE);
		
		Label lblSuccesfulPosts = new Label(shell, SWT.NONE);
		lblSuccesfulPosts.setText("Succesful Posts: 0");
		new Label(shell, SWT.NONE);
		
		Label lblTotalPosts = new Label(shell, SWT.NONE);
		lblTotalPosts.setText("Total Posts: 0");
		new Label(shell, SWT.NONE);
		
		Label lblLastRequestResult = new Label(shell, SWT.NONE);
		lblLastRequestResult.setText("Last Request Result: -1");

	}

}
